let express = require('express');
let mysql = require('./db');

let multer = require('multer');
let path = require('path')
let router = express.Router();

//配置文件上传的详细信息
let storage = multer.diskStorage({
    destination: './src/upload',
    filename: function (req, file, cb) {
        // req:请求对象
        // file: 上传文件信息
        // cb：回调函数，用于确认文件名称

        // 获取文件的后缀名
        let ext = path.extname(file.originalname)
        cb(null, 'abc' + Date.now() + ext)
    }
})

let upload = multer({
    storage: storage
})


//上传图片
router.post('/upload', upload.single('comment_img'), async (req, res) => {
    let { cid } = req.body;

    console.log(req.file);
    let img = ' http://116.62.185.51:2202/upload/' + req.file.filename;
    mysql.query(`update menu set comment_img='${img}' where cid=${cid};`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '修改数据成功',
                img
            })
        } else {
            res.send({
                code: 400,
                msg: '修改数据失败'
            })
        }
    })
})
//查询所有评论
router.get('/selAllcomment',(req, res)=>{
    let {page,size} = req.query
      // 当不传递 page 和 limit 需要设置默认值
      page = page || 1;
      size = size || 20;
      let index = (page - 1) * size;
    mysql.query(`select * from comment_dishes limit ${index},${size};`,(state, data) => {
        if (state) { 
            mysql.query(`select count(*) as total from comment_dishes;`,(state1,total)=>{
                if (state1) { 
                        res.send({
                            code: 200,
                            msg: '请求成功',
                            total: total[0].total,
                            data
                        })
                } else {
                    res.send({
                        code: 400,
                        msg: '请求错误',
                        data
                    })
                }
            })
        } else {
            res.send({
                code: 400,
                msg: '请求错误',
                data
            })
        }
    })
})

//根据用户名查询所有评论
router.get('/selAllcomment/username',(req, res)=>{
    let {page,size,username} = req.query
      // 当不传递 page 和 limit 需要设置默认值
      page = page || 1;
      size = size || 20;
      let index = (page - 1) * size;
    mysql.query(`select * from comment_dishes  where username='${username}' limit ${index},${size};`,(state, data) => {
        if (state) { 
                res.send({
                    code: 200,
                    msg: '请求成功',
                    data
                })
        } else {
            res.send({
                code: 400,
                msg: '请求错误',
                data
            })
        }
    })
})

//添加评论
router.post('/addComment', function(req, res){
    let {username,dishes_id,comment,add_time,comment_img} = req.body
    comment_img = comment_img || null
    mysql.query(`insert into comment_dishes(cid,username,dishes_id,comment,add_time,comment_img) values(Null,'${username}','${dishes_id}','${comment}','${add_time}','${comment_img}');`,(state, data)=>{
        if (state) {
            res.send({
                code:200,
                msg: '添加评论成功',
                data
            })

        } else {
            res.send({
                code: 400,
                msg: '添加评论失败',
                data
            })
        }
    })
})

//删除评论
router.post('/delComment', function(req, res){
    let {cid} = req.body
    mysql.query(`delete from comment_dishes where cid = '${cid}';`,(state, data)=>{
        if (state) {
            res.send({
                code:200,
                msg: '评论删除成功',
                data
            })

        } else {
            res.send({
                code: 400,
                msg: '评论删除失败',
                data
            })
        }
    })
})

module.exports = router